package ui;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import model.ClassRoom;
import dao.DataManager;

public class ClassManagementPanel extends JPanel {
    private JTable classTable;
    private DefaultTableModel tableModel;
    private JButton addButton;
    private JButton editButton;
    private JButton deleteButton;
    
    public ClassManagementPanel() {
        setLayout(new BorderLayout());
        initComponents();
    }
    
    private void initComponents() {
        // 创建表格
        String[] columnNames = {"ID", "班级名称", "描述"};
        tableModel = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        classTable = new JTable(tableModel);
        JScrollPane scrollPane = new JScrollPane(classTable);
        
        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        addButton = new JButton("新增班级");
        editButton = new JButton("编辑班级");
        deleteButton = new JButton("删除班级");
        
        buttonPanel.add(addButton);
        buttonPanel.add(editButton);
        buttonPanel.add(deleteButton);
        
        // 添加到面板
        add(scrollPane, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
        
        // 添加事件监听
        addButton.addActionListener(e -> showAddDialog());
        editButton.addActionListener(e -> showEditDialog());
        deleteButton.addActionListener(e -> deleteClass());
        
        // 初始化数据
        refreshTable();
    }
    
    private void refreshTable() {
        tableModel.setRowCount(0);
        for (ClassRoom classroom : DataManager.getInstance().getAllClasses()) {
            tableModel.addRow(new Object[]{
                classroom.getId(),
                classroom.getName(),
                classroom.getDescription()
            });
        }
    }
    
    private void showAddDialog() {
        JDialog dialog = new JDialog((Frame)SwingUtilities.getWindowAncestor(this), "新增班级", true);
        dialog.setLayout(new GridLayout(4, 2, 5, 5));
        
        JTextField nameField = new JTextField();
        JTextField descField = new JTextField();
        
        dialog.add(new JLabel("班级名称:"));
        dialog.add(nameField);
        dialog.add(new JLabel("描述:"));
        dialog.add(descField);
        
        JButton confirmButton = new JButton("确定");
        confirmButton.addActionListener(e -> {
            if (nameField.getText().trim().isEmpty()) {
                JOptionPane.showMessageDialog(dialog, "班级名称不能为空！");
                return;
            }
            
            int newId = DataManager.getInstance().getAllClasses().size() + 1;
            ClassRoom newClass = new ClassRoom(newId, nameField.getText(), descField.getText());
            DataManager.getInstance().addClass(newClass);
            refreshTable();
            dialog.dispose();
        });
        
        dialog.add(confirmButton);
        dialog.add(new JButton("取消") {{ addActionListener(e -> dialog.dispose()); }});
        
        dialog.pack();
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }
    
    private void showEditDialog() {
        int selectedRow = classTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "请先选择要编辑的班级！");
            return;
        }
        
        // 获取选中行的数据
        int id = (int) tableModel.getValueAt(selectedRow, 0);
        String currentName = (String) tableModel.getValueAt(selectedRow, 1);
        String currentDesc = (String) tableModel.getValueAt(selectedRow, 2);
        
        // 创建编辑对话框
        JDialog dialog = new JDialog((Frame)SwingUtilities.getWindowAncestor(this), "编辑班级", true);
        dialog.setLayout(new GridLayout(4, 2, 5, 5));
        
        // 创建输入框并设置当前值
        JTextField nameField = new JTextField(currentName);
        JTextField descField = new JTextField(currentDesc);
        
        dialog.add(new JLabel("班级名称:"));
        dialog.add(nameField);
        dialog.add(new JLabel("描述:"));
        dialog.add(descField);
        
        // 确定按钮
        JButton confirmButton = new JButton("确定");
        confirmButton.addActionListener(e -> {
            if (nameField.getText().trim().isEmpty()) {
                JOptionPane.showMessageDialog(dialog, "班级名称不能为空！");
                return;
            }
            
            // 创建新的班级对象
            ClassRoom updatedClass = new ClassRoom(id, nameField.getText(), descField.getText());
            
            // 更新数据
            // 首先需要在 DataManager 中添加更新方法
            DataManager.getInstance().updateClass(updatedClass);
            
            // 刷新表格
            refreshTable();
            dialog.dispose();
        });
        
        // 取消按钮
        JButton cancelButton = new JButton("取消");
        cancelButton.addActionListener(e -> dialog.dispose());
        
        dialog.add(confirmButton);
        dialog.add(cancelButton);
        
        dialog.pack();
        dialog.setLocationRelativeTo(this);
        dialog.setVisible(true);
    }
    
    private void deleteClass() {
        int selectedRow = classTable.getSelectedRow();
        if (selectedRow == -1) {
            JOptionPane.showMessageDialog(this, "请先选择要删除的班级！");
            return;
        }
        
        int id = (int) tableModel.getValueAt(selectedRow, 0);
        int confirm = JOptionPane.showConfirmDialog(this, 
            "确定要删除这个班级吗？", "确认删除", 
            JOptionPane.YES_NO_OPTION);
            
        if (confirm == JOptionPane.YES_OPTION) {
            DataManager.getInstance().removeClass(id);
            refreshTable();
        }
    }
} 